package com.example.dreamlist2.goal;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.Locale;

import com.example.dreamlist2.R;
import com.example.dreamlist2.StateInfo;

public class PicActivity extends AppCompatActivity {

    private final static int REQUEST_CAMERA = 123;
    private final static String IMAGE_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562328963756&di=9c0c6c839381c8314a3ce8e7db61deb2&imgtype=0&src=http%3A%2F%2Fpic13.nipic.com%2F20110316%2F5961966_124313527122_2.jpg";
    private final static String IMAGE_URL2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562328963755&di=f4aa87b95c87dc01ff0ca2c9150845c8&imgtype=0&src=http%3A%2F%2Fwww.uimaker.com%2Fuploads%2Fallimg%2F121105%2F1_121105084854_2.jpg";
    private ImageView mImageView;
    private File imagePath;
    StateInfo stateInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        stateInfo = (StateInfo)getApplication();
        setContentView(R.layout.activity_pic);



        initButton();
    }

    private void initButton() {
        //回调逻辑
        GetPhotoUtil.getInstance(PicActivity.this).setReturnPhoto(new GetPhotoUtil.ReturnPhoto() {

            @Override
            public void onGetPhotos(String[] selectImagePaths) {
                mImageView.setImageBitmap(GetPhotoUtil.getBitmapFromUri(PicActivity.this,selectImagePaths[0]));
                //txtPath.setText(selectImagePaths[0]);

                Log.i(">>>", "onGetPhotos: ====================");
                for (String s : selectImagePaths) {
                    Log.i("+", s);
                }
                Log.i(">>>", "onGetPhotos: ====================");
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(PicActivity.this, "发生错误，请在Logcat查看详情", Toast.LENGTH_SHORT);
                e.printStackTrace();
            }
        });

        mImageView = findViewById(R.id.imageView);

        findViewById(R.id.buttonCamera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openSystemCamera();
                //GetPhotoUtil.getInstance(PicActivity.this).doOpenCamera();
            }
        });

        findViewById(R.id.buttonLoad).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetPhotoUtil.getInstance(PicActivity.this).doOpenGallery();
            }
        });

        findViewById(R.id.buttonback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //GetPhotoUtil.getInstance(PicActivity.this).doOpenGallery();
                setResult(2);
                PicActivity.this.finish();
            }
        });
    }


    /**
     * 打开系统相机
     */
    private void openSystemCamera() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1 );
        }
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String imageName = String.format(Locale.getDefault(), "img_%d.jpg", System.currentTimeMillis());
        imagePath = new File(getExternalFilesDir(""), imageName);
        Uri outUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", imagePath);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
        startActivityForResult(cameraIntent, REQUEST_CAMERA);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
            Glide.with(this).load(imagePath).into(mImageView);
            HabitCompleteItem recordItem = stateInfo.getRecordin();
            recordItem.setPhotopath(imagePath);
            stateInfo.setRecordin(recordItem);
            //GetPhotoUtil.getInstance(PicActivity.this).onActivityResult(requestCode, resultCode, data);
        }
    }
}